import wxApi from '../../utils/wxApi';
import api from '../../utils/api';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    swiper: {
      type: Array,
      value: [],
      // 轮播图数据
    },
    isShowImg: {
      type: Boolean,
      value: false
    },
    bannerHeight: {
      type: Number,
      value: 644
    },
    dots: {
      type: Boolean,
      value: false
      // 是否显示指示器
    },
    isNum: {
      type: Boolean,
      value: false
      // 是否显示指示数量
    },
    interval: {
      type: Number,
      value: 5000
      // 自动切换时间间隔
    },
    duration: {
      type: Number,
      value: 500
      // 滑动动画时长
    },
    indicatorColor: {
      type: String,
      value: '#fff'
      // 指示点颜色
    },
    indicatorActiveColor: {
      type: String,
      value: '#fa3e3e'
      // 当前选中的指示点颜色
    },
    diyHeight:{
      type:String
      // 自定义高度
    },
    isDiscount:{
      type:Boolean,
      value:false
    },
    discount:{
      type:String
      // 优惠折扣
    },
    couponList:{
      type:Array,
      value:[]
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgheights: {},
    curBannerHeight: 0,
    autoplay: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindchange: function (e) {
      // console.log('e=========', e)
      let swiperArr = this.properties.swiper;
      let curnt = swiperArr[e.detail.current];
      //console.log(curnt);
      this.pauseAllVideos(curnt); //暂停全部
      this.setData({
        curBannerHeight: e.detail.current
      })
    },

    bindtransition() {
      this.triggerEvent("updateDot", {
        dots: true
      });
    },
    bindanimationfinish() {
      this.triggerEvent("updateDot", {
        dots: false
      });
    },

    pauseAllVideos: function (e) {
      let swiperArr = this.properties.swiper;
      let farr = swiperArr.filter(f => f.admemo!="")
      var count = 0;
      farr.forEach(item => {
        const context = wx.createVideoContext(item.adid + '', this);
        if(item.adid==e.adid){
          context.play();
          this.setData({
            autoplay: false
          })
        }else{
          context.pause();
          count++;
        }        
      })
      if(count==farr.length) {
        this.setData({
          autoplay: true
        })
      }


      // let swiperArr = this.properties.swiper;
      // let farr = swiperArr.filter(f=>f.admemo&&f.adid!=e.currentTarget.id)
      // console.log('farr===',farr)
      // farr.forEach(item=>{
      //   const context = wx.createVideoContext(item.adid+'', this);
      //   console.log('context==',context)
      //   context.pause();
      // })
    },

    videoPlay(e) {
      this.setData({
        autoplay: false
      })
    },

    videoPause(e) {
      this.setData({
        autoplay: true
      })
    },

    videoEnded(e) {
      let curIndex = e.currentTarget.dataset.index;
      // console.log('curIndex============',curIndex);
      // console.log('curBannerHeight============',this.data.curBannerHeight);
      if (curIndex == this.data.curBannerHeight) {
        this.setData({
          autoplay: true
        })
      }
    },

    golink(e) {
      // console.log('e=', e, this.properties.isShowImg)
      if (this.properties.isShowImg) {
        let urls = this.data.swiper.map(m => {
          return api.imageCdn + m.adimg+'?x-oss-process=image/resize,w_1000/quality,q_100/watermark,image_c2h1aXlpbi5wbmc='
        })
        let url = api.imageCdn + e.currentTarget.dataset.imgurl+'?x-oss-process=image/resize,w_1000/quality,q_100/watermark,image_c2h1aXlpbi5wbmc='
        wx.previewImage({
          urls: urls,
          current: url,
          fail() {
            wx.showToast({
              title: '预览图片失败',
              icon: 'none'
            });
          },
        });
      } else {
        const data = e.currentTarget.dataset || e.target.dataset;
        //转为小写并去除左右空格
        var str = data.url.toLowerCase();
        if (str != "") {
          if (str.indexOf('https') === 0 || str.indexOf('http') === 0) {
            wxApi.navigateTo("/pages/web/web?id=" + str);
          } else if (str.indexOf('mm://') == 0) {
            var array = str.split('_');
            if (array[0].replace("mm://", "") == 'shop') {
              wxApi.navigateTo('/pages/companydetail/index', {
                id: array[1]
              });
            }
          }else if (str.indexOf('ym://') == 0) {
            var url = str.replace("ym://", "");
            wxApi.navigateTo(url);
          } else {
            wxApi.navigateTo("/pages/goods/goods?id=" + str);
          }
        }
      }
    },

    imageLoad: function (e) { //获取图片真实宽度  
      var imgwidth = e.detail.width,
        imgheight = e.detail.height,
        //宽高比  
        ratio = imgwidth / imgheight;
      //计算的高度值  
      var viewHeight = 750 / ratio;
      var imgheight = viewHeight;
      var imgheights = this.data.imgheights;
      //把每一张图片的对应的高度记录到数组里  
      imgheights[e.target.dataset.id] = imgheight;
      this.setData({
        imgheights: imgheights
      })
    },
  }
})